import { UserAccount } from '@prisma/client';
import { TX } from '../db';

// TODO: 缓存

async function findByPhone(tx: TX, phone: string): Promise<UserAccount | null> {
  return tx.userAccount.findFirst({ where: { phone } });
}

async function findByUserId(tx: TX, userId: bigint): Promise<UserAccount | null> {
  return tx.userAccount.findFirst({ where: { userId } });
}

type CreateUserAccountData = {
  phone: string;
};

async function create(tx: TX, { phone }: CreateUserAccountData): Promise<UserAccount> {
  const now = new Date();
  return tx.userAccount.create({
    data: {
      phone,
      locked: false,
      disabled: false,
      createTime: now,
      updateTime: now,
    },
  });
}

export const userAccountService = {
  create,
  findByPhone,
  findByUserId,
};
